<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="static/layui/css/layui.css">
  <style>
    .layui-form-label {
      width: 120px !important;
    }

    .layui-input-block {
      margin-left: 150px !important;
    }
  </style>
</head>
<body>
<div class="layui-container">
  <form class="layui-form" action="" style="margin-top: 20px;" lay-filter="edit">
    <div class="layui-form-item">
      <label class="layui-form-label">节点类型</label>
      <div class="layui-input-block">
        <input type="radio" name="type" value="1" title="同级类型" checked>
        <input type="radio" name="type" value="2" title="子级类型">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">类型名称</label>
      <div class="layui-input-block">
        <input type="text" name="name" autocomplete="off" placeholder="类型名称" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">类型描述</label>
      <div class="layui-input-block">
        <input type="text" name="desc" autocomplete="off" placeholder="类型描述" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="add">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary" id="cancel">取消</button>
      </div>
    </div>
  </form>
</div>
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script src="/static/layui/layui.js"></script>
<script src="/static/js/common.js"></script>
<script>
    var layer = null;
    var form = null;
    var id = getUrlParam('id');
    var parentId = getUrlParam('parentId');
    console.log(id)
    console.log(parentId)
    layui.use(['form', 'layer'], function () {
        form = layui.form;
        layer = layui.layer;
        //监听提交
        form.on('submit(add)', function (data) {
            add(data.field);
            return false;
        });
    });
    $('#cancel').click(function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });

    function validateAdd(data) {
        if (data.name === '') {
            alert('name不能为空！');
            return false;
        }
        if (parentId == 0 && id == 0 && data.type == 2) {
            alert('当前类型为空，不能添加子节点，请添加同级节点');
            return false;
        }
        return true;
    }
    function add(data) {
        if (validateAdd(data)) {
            var url = '/doctype/add';
            if(data.type == 1) {
                // 添加同级节点, 获取父级节点
                url += '?parentId=' + parentId + '&name=' + data.name + '&desc=' + data.desc;
            }else {
                url += '?parentId=' + id + '&name=' + data.name + '&desc=' + data.desc;
            }
            $.getJSON(url, function (res) {
                if (res.code === 200) {
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index);
                }else {
                    layer.msg('添加失败');
                }
            });
        }
    }
</script>

</body>
</html>